package com.me.darkness_is_coming.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.me.darkness_is_coming.actors.*;
import com.me.darkness_is_coming.main.MainGame;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;

import com.badlogic.gdx.scenes.scene2d.Group;
import com.me.darkness_is_coming.managers.Save;

public class GameScreen implements Screen
{
    private SpriteBatch batch;
    private TextureActor background_fon;
    //-----------For Game Buttons---------------
    private Stage menu_buttons;
    private TextureActor play_button;
    private Texture pause_icon;
    private boolean game_paused;
    //----------For Game Over-----------------------
    private Texture GameOverNote;
    private boolean gameover;
    //----------For High Scores-----------------------
    private Sprite HighScoresNote;
    private float high_scores_alpha;
    private float high_scores_timer;
    private float high_scores_show_time;
    private float high_scores_end_time;
    //----------Atlases---------------------------
    private TextureAtlas CloudAtlas;
    private TextureAtlas FoodAtlas;

    private TextureRegion VioletCloudTexture1;
    private TextureRegion VioletCloudTexture2;
    private TextureRegion VioletCloudTexture3;

    private TextureRegion GreenCloudTexture1;
    private TextureRegion GreenCloudTexture2;
    private TextureRegion GreenCloudTexture3;

    private TextureRegion RedCloudTexture1;
    private TextureRegion RedCloudTexture2;
    private TextureRegion RedCloudTexture3;

    private TextureRegion RedAppleTexture;
    private TextureRegion GreenAppleTexture;
    private TextureRegion VioletAppleTexture;
    private TextureRegion RainbowtAppleTexture;
    private TextureRegion FrozenMashroomTexture;

    private TextureAtlas[] DragonAtlas;
    //----------For generator---------------------------
    int[] predsRaws;
    private int Wobjec;

    //----------Speeds---------------------------
    public static final float CloudMushroomSpeedNormal = Gdx.graphics.getHeight()*0.006f;
    public static final float CloudMushroomSpeedMax=(Gdx.graphics.getHeight()/8)*1.5f;
    public static float CloudMushroomSpeed = Gdx.graphics.getHeight()*0.006f;
    public static float CloudMushroomAcceleration = Gdx.graphics.getHeight()*0.0012f;

    public static float DarknessSpeed = Gdx.graphics.getHeight()*0.006f;
    public static float DarknessAcceleration = Gdx.graphics.getHeight()*0.0024f;
    //----------Slow---------------------------
    public static final float CloudSlowFactor = 2f;
    //----------Game resouces---------------------------
    public static int DragonRedAppleNum = 0;
    public static int DragonGreenAppleNum = 0;
    public static int DragonVioletAppleNum = 0;

    //----------GROUPS+STAGE---------------------------
    private Group CloudManager;
    private Group FoodManager;

    private Dragon dragon;
    private Darkness darkness;

    private Stage stage;
    private Stage GUI;

    //----------ELSE---------------------------
    private boolean clean;
    private float time;
    private float prev_tick;

    private float score = 0;
    private int wasted = 0;

    private int tt = 0;

    private BitmapFont font;
    private BitmapFont font_stroke;
    private BitmapFont highScore_font;
    private BitmapFont highScore_font_stroke;
    //GUi
    private TextureAtlas ScaleAtlas;

    private TextureRegion ScaleLineTex;
    private TextureRegion DarknessGUITex;
    private TextureRegion DragonGUITex;

    private GUIElement ScaleLine;
    private GUIElement DarknessGUI;
    private GUIElement DragonGUI;

    private float Distance = 0f;

    public GameScreen(SpriteBatch batch)
    {
        darkness = new Darkness();
        high_scores_timer = 0.0f;
        high_scores_show_time = 5.0f;
        high_scores_end_time = 3.0f;

        background_fon = new TextureActor(new Texture("data/Fons/PlayFon.png"), 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        GameOverNote = new Texture("data/Play/GameOver.png");
        HighScoresNote = new Sprite(new Texture("data/Play/HighScores.png"));
        HighScoresNote.setPosition(0, 0);
        HighScoresNote.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        high_scores_alpha = 1.0f;
        //canPlayMusic = false;
        //curr_volume = -1;
        game_paused = false;
        gameover = false;
        pause_icon = new Texture("data/Play/PauseIcon.png");
        play_button = new TextureActor(pause_icon, Gdx.graphics.getWidth()*0.88f, Gdx.graphics.getHeight()*0.02f, Gdx.graphics.getWidth()*0.1f, Gdx.graphics.getWidth()*0.1f);
        play_button.addListener(new InputListener()
        {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
            {
                MainGame.getInstance().showPause();
                game_paused = !game_paused;
                return false;
            }

            public void touchUp (InputEvent event, float x, float y, int pointer, int button)
            {
            }
        });
        this.batch = batch;

        //music_timer = 0;
        menu_buttons = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
        menu_buttons.addActor(play_button);
//----------------------------------------------------------------------------------------------------------------------
        Wobjec = Gdx.graphics.getWidth()/4;
        predsRaws = new int[4];
        predsRaws[0] = 0;
        predsRaws[1] = 0;
        predsRaws[2] = 0;
        predsRaws[3] = 0;
        time = 0f;
        prev_tick = 0;

        CloudAtlas = new TextureAtlas("data/Clouds/clouds.atlas");

        VioletCloudTexture1 = new TextureRegion(CloudAtlas.findRegion("VioletCloud01"));
        VioletCloudTexture2 = new TextureRegion(CloudAtlas.findRegion("VioletCloud02"));
        VioletCloudTexture3 = new TextureRegion(CloudAtlas.findRegion("VioletCloud03"));

        GreenCloudTexture1 = new TextureRegion(CloudAtlas.findRegion("GreenCloud01"));
        GreenCloudTexture2 = new TextureRegion(CloudAtlas.findRegion("GreenCloud02"));
        GreenCloudTexture3 = new TextureRegion(CloudAtlas.findRegion("GreenCloud03"));

        RedCloudTexture1 = new TextureRegion(CloudAtlas.findRegion("RedCloud01"));
        RedCloudTexture2 = new TextureRegion(CloudAtlas.findRegion("RedCloud02"));
        RedCloudTexture3 = new TextureRegion(CloudAtlas.findRegion("RedCloud03"));


        FoodAtlas               = new TextureAtlas("data/Food/food.atlas");
        RedAppleTexture         = new TextureRegion(FoodAtlas.findRegion("AppleRed"));
        GreenAppleTexture       = new TextureRegion(FoodAtlas.findRegion("AppleGreen"));
        VioletAppleTexture      = new TextureRegion(FoodAtlas.findRegion("AppleViolet"));
        RainbowtAppleTexture    = new TextureRegion(FoodAtlas.findRegion("AppleRainbow"));
        FrozenMashroomTexture   = new TextureRegion(FoodAtlas.findRegion("MushroomFrozen"));

        DragonAtlas = new TextureAtlas[4];
        DragonAtlas[0] = new TextureAtlas("data/Dragons/CyanDragon/cyandragon.atlas");
        DragonAtlas[1] = new TextureAtlas("data/Dragons/GreenDragon/greendragon.atlas");
        DragonAtlas[2] = new TextureAtlas("data/Dragons/RedDragon/reddragon.atlas");
        DragonAtlas[3] = new TextureAtlas("data/Dragons/VioletDragon/violetdragon.atlas");

        FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/CooperBlackStd.otf"));
        FreeTypeFontGenerator.FreeTypeFontParameter font_parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        font_parameter.size = (int)(Gdx.graphics.getWidth()*0.063f);
        font = gen.generateFont(font_parameter);
        font.setColor(240f/255f, 232f/255f, 3f/255f, 1.0f);
        font_stroke = gen.generateFont(font_parameter);
        font_stroke.setColor(74f/255f, 46f/255f, 5f/255f, 1.0f);
        font_parameter.size = (int)(Gdx.graphics.getWidth()*0.08f);
        highScore_font = gen.generateFont(font_parameter);
        highScore_font.setColor(240f/255f, 232f/255f, 3f/255f, 1.0f);
        highScore_font_stroke = gen.generateFont(font_parameter);
        highScore_font_stroke.setColor(74f/255f, 46f/255f, 5f/255f, 1.0f);

        time = 0f;
        prev_tick = 0f;

        CloudManager = new Group();
        FoodManager = new Group();
        dragon = new Dragon(DragonAtlas, Dragon.ColorType.RED);
        darkness = new Darkness();
        ///////////////////////////////////////////////////
        ///////////////////////////////////////////////////
        ///////////Scene

        ScaleAtlas = new TextureAtlas("data/Scale/Scale.atlas");
        ScaleLineTex =  new TextureRegion(ScaleAtlas.findRegion("ScaleLine"));
        DarknessGUITex = new TextureRegion(ScaleAtlas.findRegion("DarknessImage"));
        DragonGUITex = new TextureRegion(ScaleAtlas.findRegion("DragonIcon"));


        ScaleLine = new GUIElement(ScaleLineTex, Gdx.graphics.getWidth()*0.1f,Gdx.graphics.getHeight()*0.9f,Gdx.graphics.getWidth()*0.8f,Gdx.graphics.getHeight()*0.03f);
        DragonGUI = new GUIElement(DragonGUITex, Gdx.graphics.getWidth()*0.85f,Gdx.graphics.getHeight()*0.885f,Gdx.graphics.getHeight()*0.06f,Gdx.graphics.getHeight()*0.06f);


        DarknessGUI = new GUIElement(DarknessGUITex, (Gdx.graphics.getWidth()-Gdx.graphics.getHeight()*0.06f+(darkness.getY()-dragon.getY())*(Gdx.graphics.getWidth()-Gdx.graphics.getHeight()*0.12f)/(4*Gdx.graphics.getHeight())),Gdx.graphics.getHeight()*0.885f,Gdx.graphics.getHeight()*0.09f,Gdx.graphics.getHeight()*0.06f);


        GUI = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));

        for(int i = 0 ; i < 5 ; ++i){
            GUI.addActor(new Food(RedAppleTexture   ,Gdx.graphics.getHeight()*0.027f+i*Gdx.graphics.getHeight()*0.027f,Gdx.graphics.getHeight()*0.06f,Gdx.graphics.getHeight()*0.06f,Gdx.graphics.getHeight()*0.06f, Food.FoodType.APPLE, Food.FoodColor.RED   ));
            GUI.addActor(new Food(GreenAppleTexture ,Gdx.graphics.getHeight()*0.027f+i*Gdx.graphics.getHeight()*0.027f,Gdx.graphics.getHeight()*0.1f,Gdx.graphics.getHeight()*0.06f,Gdx.graphics.getHeight()*0.06f, Food.FoodType.APPLE, Food.FoodColor.GREEN ));
            GUI.addActor(new Food(VioletAppleTexture,Gdx.graphics.getHeight()*0.027f+i*Gdx.graphics.getHeight()*0.027f,Gdx.graphics.getHeight()*0.14f,Gdx.graphics.getHeight()*0.06f,Gdx.graphics.getHeight()*0.06f, Food.FoodType.APPLE, Food.FoodColor.VIOLET));
        }
        for(Actor actor:GUI.getRoot().getChildren()){
            actor.setVisible(false);
        }
        GUI.addActor(ScaleLine);
        GUI.addActor(DragonGUI);
        GUI.addActor(DarknessGUI);

        stage = new Stage(new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
        stage.addActor(background_fon);
        stage.addActor(CloudManager);
        stage.addActor(FoodManager);
        stage.addActor(dragon);
        stage.addActor(darkness);
    }
    public void clean(){
        high_scores_alpha = 1.0f;
        game_paused = false;
        gameover = false;
//----------------------------------------------------------------------------------------------------------------------
        predsRaws[0] = 0;
        predsRaws[1] = 0;
        predsRaws[2] = 0;
        predsRaws[3] = 0;
        time = 0f;
        prev_tick = 0;

        time = 0f;
        prev_tick = 0f;


        DragonRedAppleNum = 0;
        DragonGreenAppleNum = 0;
        DragonVioletAppleNum = 0;

        CloudMushroomSpeed = Gdx.graphics.getHeight()*0.006f;
        CloudMushroomAcceleration = Gdx.graphics.getHeight()*0.0012f;

        DarknessSpeed = Gdx.graphics.getHeight()*0.006f;
        DarknessAcceleration = Gdx.graphics.getHeight()*0.0024f;

        CloudManager.clear();
        FoodManager.clear();


        for(int i = 0; i < 15 ;++i){
            GUI.getRoot().getChildren().get(i).setVisible(false);
        }
        dragon.setX(Gdx.graphics.getWidth()*0.5f);
        darkness.clear();
        Distance = 0f;
    }
    public boolean isclean(){
        return CloudManager.getChildren().size == 0;
    }
    //----------Vaxtel's Generator (Child of Satan)--------------------------------
    private void vaxtel_generator()
    {
        /*0 пусто обл
        *1 зелен обл
        *2 красн обл
        *3 фиолет обл
        *4 пуст
        *5 зелен ябл
        *6 красн ябл
        *7 фиолет ябл
        *8 пуст
        *9 гриб заморозки
        *10 радужн гриб
        *11 градиентное яблоко
        */
        if (prev_tick+5*CloudMushroomSpeedNormal/CloudMushroomSpeed < time) {
            int[][] Raws=new int[4][3];
            for(int i=0; i<4; i++)
            {
                if(Math.round((float) Math.random()*4)!=0)//0-еда. 1,2- облака.
                {//облака
                    Raws[i][0]=Math.round((float) Math.random()*4);
                }
                else
                {
                    if(Math.round((float) Math.random()*6)==0)
                    {
                        Raws[i][0]=Math.round((float) Math.random()*5+8);

                    }
                    else
                    {
                        Raws[i][0]=Math.round((float) Math.random()*5+4);
                    }
                }

            }
            for(int i=0; i<4; i++)
            {
                if(Math.round((float) Math.random()*7)==0)
                {
                    Raws[i][1]=Math.round((float) Math.random()*5+8);

                }
                else
                {
                    Raws[i][1]=Math.round((float) Math.random()*5+4);
                }
            }
            for(int i=0; i<4; i++)
            {
                if(Math.round((float) Math.random()*3)!=0)//0-������. 1,2- ���.
                {

                    if(Math.round((float) Math.random()*6)==0)
                    {
                        Raws[i][2]=Math.round((float) Math.random()*5+8);

                    }
                    else
                    {
                        Raws[i][2]=Math.round((float) Math.random()*5+4);
                    }
                }
                else
                {
                    Raws[i][2]=Math.round((float) Math.random()*4);
                }

            }
            boolean Raw1Empty=false;
            boolean Raw2Empty=false;
            int kfmk;
            for(int i=0; i<4; i++)
            {
                if((Raws[i][2]==9)||(Raws[i][2]==10)||(Raws[i][2]==5)||(Raws[i][2]==6)||(Raws[i][2]==7))  if((wasted>50)&&(wasted<300)) {kfmk=wasted; if(Math.round((float) Math.random()*Math.floor(kfmk/50f))!=0)   Raws[i][2]=Math.round((float) Math.random()*4);} else {if((wasted>50)) {kfmk=450; if(Math.round((float) Math.random()*Math.floor(kfmk/50f))!=0)   Raws[i][2]=Math.round((float) Math.random()*4);}}
                if((Raws[i][1]==9)||(Raws[i][1]==10)||(Raws[i][1]==5)||(Raws[i][1]==6)||(Raws[i][1]==7))  if((wasted>50)&&(wasted<300)) {kfmk=wasted; if(Math.round((float) Math.random()*Math.floor(kfmk/50f))!=0)   Raws[i][2]=Math.round((float) Math.random()*4);} else {if((wasted>50)) {kfmk=450; if(Math.round((float) Math.random()*Math.floor(kfmk/50f))!=0)   Raws[i][1]=Math.round((float) Math.random()*4);}}
                if((Raws[i][0]==9)||(Raws[i][0]==10)||(Raws[i][0]==5)||(Raws[i][0]==6)||(Raws[i][0]==7))  if((wasted>50)&&(wasted<300)) {kfmk=wasted; if(Math.round((float) Math.random()*Math.floor(kfmk/50f))!=0)   Raws[i][2]=Math.round((float) Math.random()*4);} else {if((wasted>50)){kfmk=450; if(Math.round((float) Math.random()*Math.floor(kfmk/50f))!=0)   Raws[i][0]=Math.round((float) Math.random()*4);}}
                if((Raws[i][2]==9)||(Raws[i][2]==10)) if(Math.round((float) Math.random()*3)!=0) Raws[i][2]=Math.round((float) Math.random()*5+4);
                if((Raws[i][1]==9)||(Raws[i][1]==10)) if(Math.round((float) Math.random()*3)!=0) Raws[i][1]=Math.round((float) Math.random()*5+4);
                if((Raws[i][0]==9)||(Raws[i][0]==10)) if(Math.round((float) Math.random()*3)!=0) Raws[i][0]=Math.round((float) Math.random()*5+4);
                if(Raws[i][0]==0) Raw1Empty=true;
                if(Raws[i][2]==0) Raw2Empty=true	;
            }
            if(!Raw1Empty) Raws[Math.round((float)Math.random()*3)][0]=0;
            if(!Raw2Empty) Raws[Math.round((float)Math.random()*3)][2]=0;

            int RawChoice=0;
            switch(Math.round((float)Math.random()*4))
            {
                case 0: RawChoice=0;  break;
                case 1: RawChoice=1;  break;
                case 2: RawChoice=2; break;
                case 4: RawChoice=2; break;
                case 3: RawChoice=0; break;

            }

            for(int i=0; i<4; i++)
            {

                if((Raws[i][2]==5)||(Raws[i][2]==6)||(Raws[i][2]==7)) if(Math.round((float) Math.random()*2)==0) Raws[i][2]=0;
                if((Raws[i][1]==5)||(Raws[i][1]==6)||(Raws[i][1]==7)) if(Math.round((float) Math.random()*2)!=0) Raws[i][1]=0;
                if((Raws[i][RawChoice]!=0)&(Raws[i][RawChoice]!=4)&(Raws[i][RawChoice]!=8)) if((predsRaws[i]!=0)&(predsRaws[i]!=4)&(predsRaws[i]!=8)) Raws[i][RawChoice]=0;

            }
            for(int i=0; i<4; i++)
            {
                predsRaws[i]=Raws[i][RawChoice];
            }
            int  h = (int)1.1f*Gdx.graphics.getHeight();

            int xCoord=0;
            for(int i=0; i<4; i++)
            {

                switch(Raws[i][RawChoice] )
                {
                    case 0: break;
                    case 1:
                    {
                        Cloud VioletCloud;
                        switch(Math.round((float) Math.random()*5) )
                        {
                            case 0:
                            {
                                VioletCloud= new Cloud(GreenCloudTexture1, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.GREEN);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 1:
                            {
                                VioletCloud = new Cloud(GreenCloudTexture2, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.GREEN);
                                CloudManager.addActor(VioletCloud);

                                break;
                            }
                            case 2:
                            {
                                VioletCloud = new Cloud(GreenCloudTexture3, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.GREEN);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 3:
                            {
                                VioletCloud= new Cloud(GreenCloudTexture1, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.GREEN);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 4:
                            {
                                VioletCloud = new Cloud(GreenCloudTexture2, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.GREEN);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            default:
                        }


                        break;
                    }
                    case 2:
                    {
                        Cloud VioletCloud;
                        switch(Math.round((float) Math.random()*5) )
                        {
                            case 0:
                            {
                                VioletCloud= new Cloud(RedCloudTexture1, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.RED);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 1:
                            {
                                VioletCloud = new Cloud(RedCloudTexture2, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.RED);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 2:
                            {
                                VioletCloud = new Cloud(RedCloudTexture3, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.RED);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 3:
                            {
                                VioletCloud= new Cloud(RedCloudTexture1, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.RED);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 4:
                            {
                                VioletCloud = new Cloud(RedCloudTexture2, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.RED);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            default:
                        }


                        break;
                    }
                    case 3:
                    {
                        Cloud VioletCloud;
                        switch(Math.round((float) Math.random()*5) )
                        {
                            case 0:
                            {
                                VioletCloud= new Cloud(VioletCloudTexture1, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.VIOLET);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 1:
                            {
                                VioletCloud = new Cloud(VioletCloudTexture2, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.VIOLET);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 2:
                            {
                                VioletCloud = new Cloud(VioletCloudTexture3, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.VIOLET);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 3:
                            {
                                VioletCloud= new Cloud(VioletCloudTexture1, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.VIOLET);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            case 4:
                            {
                                VioletCloud = new Cloud(VioletCloudTexture2, xCoord, h, Wobjec, Gdx.graphics.getHeight()*0.06f, Cloud.CloudType.VIOLET);
                                CloudManager.addActor(VioletCloud);
                                break;
                            }
                            default:
                        }

                        break;
                    }

                    case 4: break;
                    case 5:
                    {

                        Food GreenApple = new Food(GreenAppleTexture, xCoord+Gdx.graphics.getWidth()/11, h, Wobjec*40/75, Wobjec*40/75, Food.FoodType.APPLE, Food.FoodColor.GREEN );
                        FoodManager.addActor(GreenApple);
                        break;
                    }
                    case 6:
                    {
                        Food RedApple = new Food(RedAppleTexture, xCoord+Gdx.graphics.getWidth()/11, h, Wobjec*40/75, Wobjec*40/75, Food.FoodType.APPLE, Food.FoodColor.RED);
                        FoodManager.addActor(RedApple);
                        break;
                    }
                    case 7:
                    {
                        Food VioletApple = new Food(VioletAppleTexture, xCoord+Gdx.graphics.getWidth()/11, h, Wobjec*40/75, Wobjec*40/75, Food.FoodType.APPLE, Food.FoodColor.VIOLET );
                        FoodManager.addActor(VioletApple);
                        break;
                    }
                    case 8: break;
                    case 9:
                    {
                        Food FrozenMashroom = new Food(FrozenMashroomTexture, xCoord+Gdx.graphics.getWidth()/11, h, Wobjec*40/75, Wobjec*40/75,Food.FoodType.MUSHROOM, Food.FoodColor.FROZEN );
                        FoodManager.addActor(FrozenMashroom);
                        break;
                    }
                    case 10:
                    {
                        Food  RainbowMashroom = new Food( FrozenMashroomTexture, xCoord+Gdx.graphics.getWidth()/11, h, Wobjec*40/75, Wobjec*40/75,Food.FoodType.MUSHROOM, Food.FoodColor.FROZEN );
                        FoodManager.addActor(RainbowMashroom);
                        break;
                    }
                    case 11:
                    {
                        Food RainbowtApple = new Food(RainbowtAppleTexture, xCoord+Gdx.graphics.getWidth()/11, h, Wobjec*40/75, Wobjec*40/75,Food.FoodType.APPLE, Food.FoodColor.RAINBOW );
                        FoodManager.addActor(RainbowtApple);
                        break;

                    }
                    default:
                }

                xCoord+=Wobjec;
            }
            prev_tick=time;
        }
    }
    //-------------------------------------------------------------------------
    @Override
    public void render(float delta)
    {
        darkness.UpdateDarkness(gameover, delta);
        //-----------Work with background music-----------
        /*if (curr_volume != option_screen.volume_slider.getValue())
        {
            curr_volume = option_screen.volume_slider.getValue();
            if (canPlayMusic) Jukebox.loop("bg_music", curr_volume * 0.01f);
        }
        if (canPlayMusic != option_screen.musicCheckBox.getChecked())
        {
            if (canPlayMusic) Jukebox.stop("bg_music");
            else Jukebox.loop("bg_music", curr_volume * 0.01f);
            canPlayMusic = !canPlayMusic;
        }
        else music_timer += delta;
        */

        time += delta;

        if((!gameover) && (darkness.getY()>-0.7f*Gdx.graphics.getHeight()))
        {
            gameover = true;
            Save.game_data.addHighScore(Math.round(Distance));
            Save.save();
        }
        if(!gameover)
        {
            Distance += 800*CloudMushroomSpeed*delta/Gdx.graphics.getHeight();
            vaxtel_generator();
            CloudMushroomSpeed += CloudMushroomAcceleration* delta*1.25f;
            if(CloudMushroomSpeed < CloudMushroomSpeedNormal)CloudMushroomSpeed = CloudMushroomSpeedNormal;
            if(CloudMushroomSpeed > CloudMushroomSpeedMax)CloudMushroomSpeed = CloudMushroomSpeedMax;

            for(Actor cloud: CloudManager.getChildren()){
                if(((Cloud)cloud).getType() == Cloud.CloudType.WHITE){
                    if(cloud.getY()<darkness.getY()+darkness.getHeight()){
                        DarknessSpeed *= 1-((Cloud) cloud).getPower()/5;
                    }else continue;
                }
                if((cloud).getY()<-Gdx.graphics.getHeight()*0.1f) {
                    CloudManager.removeActor(cloud);
                    ++wasted;
                    break;
                }else{
                    if(((Cloud)cloud).getColider().overlaps(dragon.getColider())){
                        if(((Cloud) cloud).getType() == Cloud.CloudType.RED && dragon.getColorType() == Dragon.ColorType.RED ){
                            if(DragonRedAppleNum > 0){
                                for(int i = 0 ; i < DragonRedAppleNum;++i){
                                    GUI.getRoot().getChildren().get(3*i).setVisible(false);
                                }
                                ((Cloud) cloud).setTextureRegion(CloudAtlas.findRegion("WhiteCloud0"+((int)Math.round(Math.random()*5)+1)));
                                ((Cloud) cloud).setFColor(Cloud.CloudType.WHITE);
                                ((Cloud) cloud).setPower(DragonRedAppleNum);
                                DragonRedAppleNum = 0;
                            }else{
                                CloudManager.removeActor(cloud);
                            }
                            break;
                        }
                        if(((Cloud) cloud).getType() == Cloud.CloudType.GREEN && dragon.getColorType() == Dragon.ColorType.GREEN ){
                            if(DragonGreenAppleNum > 0){
                                for(int i = 0 ; i < DragonGreenAppleNum;++i){
                                    GUI.getRoot().getChildren().get(1+3*i).setVisible(false);
                                }
                                ((Cloud) cloud).setTextureRegion(CloudAtlas.findRegion("WhiteCloud0"+((int)Math.round(Math.random()*5)+1)));
                                ((Cloud) cloud).setFColor(Cloud.CloudType.WHITE);
                                ((Cloud) cloud).setPower(DragonGreenAppleNum);
                                DragonGreenAppleNum = 0;
                            }else{
                                CloudManager.removeActor(cloud);
                            }
                            break;

                        }
                        if(((Cloud) cloud).getType() == Cloud.CloudType.VIOLET && dragon.getColorType() == Dragon.ColorType.VIOLET ){
                            if(DragonVioletAppleNum > 0){
                                for(int i = 0 ; i < DragonVioletAppleNum;++i){
                                    GUI.getRoot().getChildren().get(2+3*i).setVisible(false);
                                }
                                ((Cloud) cloud).setTextureRegion(CloudAtlas.findRegion("WhiteCloud0"+((int)Math.round(Math.random()*5)+1)));
                                ((Cloud) cloud).setFColor(Cloud.CloudType.WHITE);
                                ((Cloud) cloud).setPower(DragonVioletAppleNum);
                                DragonVioletAppleNum = 0;
                            }else{
                                CloudManager.removeActor(cloud);
                            }
                            break;
                        }
                        CloudMushroomSpeed/=CloudSlowFactor;
                        CloudManager.removeActor(cloud);
                        break;
                    }
                }
            }
            for(Actor food: FoodManager.getChildren()){
                if(((Food)food).getY() <-Gdx.graphics.getHeight()*0.1f) {
                    FoodManager.removeActor(food);
                    ++wasted;
                    break;
                }else{
                    if(((Food)food).getColider().overlaps(dragon.getColider())){
                        if(((Food)food).getFType() == Food.FoodType.APPLE){
                            switch (((Food)food).getFColor()){
                                case RED:
                                    if(DragonRedAppleNum<5){
                                        GUI.getRoot().getChildren().get(3*DragonRedAppleNum).setVisible(true);
                                        ++DragonRedAppleNum;
                                    }
                                    break;
                                case VIOLET:
                                    if(DragonVioletAppleNum<5){
                                        GUI.getRoot().getChildren().get(2+3*DragonVioletAppleNum).setVisible(true);
                                        ++DragonVioletAppleNum;
                                    }
                                    break;
                                case GREEN:

                                    if(DragonGreenAppleNum<5){
                                        GUI.getRoot().getChildren().get(1+3*DragonGreenAppleNum).setVisible(true);
                                        ++DragonGreenAppleNum;
                                    }
                                    break;
                                case RAINBOW:
                                    if(DragonRedAppleNum<5){
                                        GUI.getRoot().getChildren().get(3*DragonRedAppleNum).setVisible(true);
                                        ++DragonRedAppleNum;
                                    }
                                    if(DragonGreenAppleNum<5) {
                                        GUI.getRoot().getChildren().get(1+3*DragonGreenAppleNum).setVisible(true);
                                        ++DragonGreenAppleNum;
                                    }
                                    if(DragonVioletAppleNum<5){
                                        GUI.getRoot().getChildren().get(2+3*DragonVioletAppleNum).setVisible(true);
                                        ++DragonVioletAppleNum;
                                    }
                                    break;
                            }
                        }else{
                            switch (((Food)food).getFColor()){
                                case FROZEN:
                                    darkness.addPenalti(5f);
                                    //тьма замерзает
                                    break;
                                case RAINBOW:
                                    //черно белыый
                                    break;
                            }
                        }

                        FoodManager.removeActor(food);
                        break;
                    }

                }
            }


            float DarkX = (Gdx.graphics.getWidth()-Gdx.graphics.getHeight()*0.06f+(darkness.getY()-dragon.getY())*(Gdx.graphics.getWidth()-Gdx.graphics.getHeight()*0.12f)/(4*Gdx.graphics.getHeight()));
            if(DarkX<0.05f*Gdx.graphics.getWidth())DarknessGUI.setX(0.05f*Gdx.graphics.getWidth());
            else DarknessGUI.setX(DarkX);

            stage.act(delta);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            Gdx.gl.glClearColor(0.074509804f,0.77647059f,0.9254902f,1f);
            stage.draw();
            GUI.draw();
            stage.getBatch().begin();
            font_stroke.draw(stage.getBatch(),"Score: "+Integer.toString(Math.round(Distance)),0.02f*Gdx.graphics.getWidth() + 0.002f*Gdx.graphics.getWidth(),Gdx.graphics.getHeight()*0.8f - Gdx.graphics.getHeight()*0.002f);
            font.draw(stage.getBatch(),"Score: "+Integer.toString(Math.round(Distance)),0.02f*Gdx.graphics.getWidth(),Gdx.graphics.getHeight()*0.8f);
            stage.getBatch().end();
        }else{
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            Gdx.gl.glClearColor(0.074509804f,0.77647059f,0.9254902f,1f);
            stage.draw();
            //GUI.draw();
            //stage.getBatch().begin();
            //font.draw(stage.getBatch(),"Score:"+Integer.toString(Math.round(Distance)),0.02f*Gdx.graphics.getWidth(),Gdx.graphics.getHeight()*0.8f);
            //stage.getBatch().end();

        }


        //----------Draw High Scores and Game Over--------------------------------
        batch.begin();
        if ((gameover)&& (!darkness.darkness_coming_animation))
        {
            if ((Save.game_data.isRecord(Math.round(Distance))) && (high_scores_timer + delta <= high_scores_show_time))
            {
                if ((high_scores_timer + delta > high_scores_end_time))
                    high_scores_alpha = 1.0f - (high_scores_timer - high_scores_end_time)/(high_scores_show_time - high_scores_end_time);
                //System.out.println(high_scores_alpha);
                HighScoresNote.draw(batch, high_scores_alpha);
                highScore_font.setColor(240f/255f, 232f/255f, 3f/255f, high_scores_alpha);
                highScore_font_stroke.setColor(74f/255f, 46f/255f, 5f/255f, high_scores_alpha);
                highScore_font_stroke.draw(batch, Integer.toString(Math.round(Distance)), Gdx.graphics.getWidth()*0.346f, Gdx.graphics.getHeight()*0.596f);
                highScore_font.draw(batch, Integer.toString(Math.round(Distance)), Gdx.graphics.getWidth() * 0.35f, Gdx.graphics.getHeight() * 0.6f);
                high_scores_timer += delta;
            }
            else batch.draw(GameOverNote, Gdx.graphics.getWidth()*0.272f, Gdx.graphics.getHeight()*0.42f, Gdx.graphics.getWidth()*0.46f, Gdx.graphics.getHeight()*0.16f);
        }
        batch.end();
        menu_buttons.act(delta);
        menu_buttons.draw();
    }
    @Override
    public void resize(int width, int height)
    {

    }
    @Override
    public void show()
    {
        Gdx.input.setInputProcessor(stage);
        Gdx.input.setInputProcessor(menu_buttons);
    }
    @Override
    public void hide()
    {
        //
    }
    @Override
    public void pause()
    {
        //
    }
    @Override
    public void resume()
    {
        //
    }
    @Override
    public void dispose()
    {
        background_fon.dispose();
        pause_icon.dispose();
        GameOverNote.dispose();
        play_button.dispose();
        dragon.dispose();
        //----------------------
        font.dispose();
        font_stroke.dispose();
        highScore_font.dispose();
        highScore_font_stroke.dispose();

        menu_buttons.clear();
        stage.clear();
        GUI.clear();

        FoodAtlas.dispose();
        ScaleAtlas.dispose();
        CloudAtlas.dispose();
        for(int i = 0; i < DragonAtlas.length; ++i)
        {
            DragonAtlas[i].dispose();
        }
    }
}
